﻿using ConmonDateModel.ResponseBase;
using DateDBControl.Model;
using MyLogProject;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ServiceControl.Helps
{
    public class ApiActionAuth
    {
        public static ResponseBody OnAuthorization(string sendToken)
        {
            var ResponseItem = new ResponseBody();
            //url获取token 
            try
            {
                LogManager.WriteLog(LogTypeEnum.Normal, "OnAuthorization", $"开始解析Token");
                var now = Math.Round((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds + 5);//当前的时间戳
                var dictPayload = DecodeToken(sendToken);

                if (dictPayload == null)
                {
                    ResponseItem.Code = ErrorCode.CommonError;
                    ResponseItem.Description = "Token 过期";
                    LogManager.WriteLog(LogTypeEnum.Normal, "OnAuthorization", $"Token 过期A");
                    return ResponseItem;
                    //NO token
                }
                LogManager.WriteLog(LogTypeEnum.Normal, "OnAuthorization", $"当前时间戳:{now}");
                double iat = dictPayload["iat"];
                double exp = dictPayload["exp"];
                LogManager.WriteLog(LogTypeEnum.Normal, "OnAuthorization", $"iat时间戳:{iat}");
                LogManager.WriteLog(LogTypeEnum.Normal, "OnAuthorization", $"exp时间戳:{exp}");
                //检查令牌的有效期
                if (!(now>=iat  && now <= exp))//如果当前时间戳不再Token声明周期范围内，则返回Token过期
                {
                    //Token过期
                    ResponseItem.Code = ErrorCode.CommonError;
                    ResponseItem.Description = "Token 过期";
                    LogManager.WriteLog(LogTypeEnum.Normal, "OnAuthorization", $"Token 过期B");
                    return ResponseItem;
                }
                //获取Token的自定义键值对
                string _username = dictPayload["UserName"];
                string _password = dictPayload["UserPwd"];
                string _role = dictPayload["UserRole"];
                //把toke用户数据放到 HttpContext.Current.User 里
                UserModel clientUserData = new UserModel()
                {
                    username = _username,
                    password = _password,
                    role = _role

                };
                ResponseItem.Code = ErrorCode.OK;
                ResponseItem.Data = clientUserData;
                LogManager.WriteLog(LogTypeEnum.Normal, "OnAuthorization", $"Token 校验完毕");
            }
            catch (Exception ex)
            {
                LogManager.WriteLog(LogTypeEnum.Normal, "OnAuthorization", $"{ex.Message}");
                ResponseItem.Code = ErrorCode.Unknown;
                ResponseItem.Description= ex.Message;
            }
            return ResponseItem;
        }

        private static IDictionary<string, dynamic> DecodeToken(string token)
        {
            try
            {
                var dictPayload = JWT.JsonWebToken.DecodeToObject(token, TokenHelper.SecretKey) as IDictionary<string, dynamic>;
                return dictPayload;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
